/**
 * @api {get} /api/admin/v1/articles/analyse 文章统计
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章统计
 * @apiPermission 管理员(查看文章权限)
 * @apiVersion 1.0.0
 */
exports.analyse = function ({}, {models: {Articles}}) {
    return Articles.analyseV2();
}

/**
 * @api {get} /api/admin/v1/articles/browse 文章列表
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章列表
 * @apiPermission 管理员(查看文章权限)
 * @apiVersion 1.0.0
 */
exports.browse = function ({query}, {models: {Articles}, controller}) {
    return Articles.findAndCountAll(controller.pagination({
        params: query,
        filters: ['type', 'status'],
        likes: ['slug', 'title', 'keywords']
    }));
}

/**
 * @api {get} /api/admin/v1/articles/load 文章详情
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章详情
 * @apiPermission 管理员(查看文章权限)
 * @apiVersion 1.0.0
 */
exports.load = function ({query}, {models: {Articles}}) {
    return Articles.findOne({where: {id: query.id}});
}

/**
 * @api {post} /api/admin/v1/articles/add 文章添加
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章添加
 * @apiPermission 管理员(添加文章权限)
 * @apiVersion 1.0.0
 */
exports.add = function ({body}, {models: {Articles}}) {
    return Articles.create(body).then(function (data) {
        return {success: true, data: data};
    });
}

/**
 * @api {post} /api/admin/v1/articles/edit 文章编辑
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章编辑
 * @apiPermission 管理员(编辑文章权限)
 * @apiVersion 1.0.0
 */
exports.edit = function ({body}, {models: {Articles}}) {
    let where = {id: body.id};
    delete body.id;
    return Articles.update(body, {where: where}).then(function (result) {
        if (!result[0]) {
            return {success: false};
        }
        return Articles.findOne({where: where}).then(function (article) {
            return {success: true, data: article};
        });
    });
}

/**
 * @api {post} /api/admin/v1/articles/edit 文章删除
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章删除
 * @apiPermission 管理员(删除文章权限)
 * @apiVersion 1.0.0
 */
exports.delete = function ({body}, {models: {Articles}}) {
    let where = {id: body.id};
    return Articles.destroy({where: where}).then(function (count) {
        return {success: (count > 0)};
    });
}

/**
 * @api {post} /api/admin/v1/articles/image_add 文章添加图片
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章添加图片
 * @apiPermission 管理员(编辑文章权限)
 * @apiVersion 1.0.0
 */
exports.imageAdd = function ({body}, {models: {Images, Articles}}) {
    let {type, reference} = body;
    if (!type || !reference) {
        return {success: false, message: '参数错误'};
    }
    return Articles.findOne({
        where: {id: reference},
        attributes: ['id', 'image', 'banner']
    }).then(function (article) {
        if (!article) {
            return {success: false, message: '文章不存在'};
        }
        return Images.create(body).then(function (image) {
            if (type == 'ArticleImage' && !article.image) {
                article.image = body.image;
                return Articles.update({image: body.image}, {where: {id: article.id}}).then(function () {
                    return {success: true, data: image, image: 1};
                });
            }
            if (type == 'ArticleBanner' && !article.banner) {
                article.banner = body.image;
                return Articles.update({banner: body.image}, {where: {id: article.id}}).then(function () {
                    return {success: true, data: image, banner: 1};
                });
            }
            return {success: true, data: image};
        });
    });
}

/**
 * @api {post} /api/admin/v1/articles/image_edit 文章编辑图片
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章编辑图片
 * @apiPermission 管理员(编辑文章权限)
 * @apiVersion 1.0.0
 */
exports.imageEdit = function ({body}, {models: {Images, Articles}}) {
    let {id, operation} = body;
    switch (operation) {
        case 'remove':
            return Images.destroy({where: {id: id}}).then(function (count) {
                return {success: (count > 0)};
            });
            break;
        case 'banner':
            return Images.findOne({where: {id: id}}).then(function (image) {
                if (!image) {
                    return {success: false, message: '图片不存在'}
                }
                return Articles.update({banner: image.image}, {where: {id: image.reference}}).then(function () {
                    return {success: true, data: image};
                });
            });
            break;
        case 'image':
            return Images.findOne({where: {id: id}}).then(function (image) {
                if (!image) {
                    return {success: false, message: '图片不存在'}
                }
                return Articles.update({image: image.image}, {where: {id: image.reference}}).then(function () {
                    return {success: true, data: image};
                });
            });
            break;
    }
}

/**
 * @api {get} /api/admin/v1/articles/image_browse 文章浏览图片
 * @apiGroup AdminArticles
 * @apiDescription 文章管理-文章浏览图片
 * @apiPermission 管理员(编辑文章权限)
 * @apiVersion 1.0.0
 */
exports.imageBrowse = function ({query}, {models: {Images}, controller}) {
    return Images.findAndCountAll(controller.pagination({
        params: query,
        filters: ['type', 'reference'],
        order: [['order_by', 'asc']],
        attributes: ['id', 'type', 'reference', 'image']
    }));
}